package com.example.lj.uav.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.bean.BatteryItem;
import com.example.lj.uav.widget.BatteryView;

import java.util.List;

import dji.common.battery.ConnectionState;
import dji.sdk.battery.Battery;

public class BatteryInfoAdapter extends BaseAdapter {
    private List<BatteryItem> dataList;
    private Context context;
    private LayoutInflater inflater;

    public BatteryInfoAdapter(List<BatteryItem> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = inflater.inflate(R.layout.item_battery_info_firs,viewGroup);
            holder = new ViewHolder();
            holder.tv_title = view.findViewById(R.id.tv_item_battery_info_name);
            holder.tv_recycle_times = view.findViewById(R.id.tv_item_battery_info_recycle_times);
            holder.ll_container = view.findViewById(R.id.ll_item_battery_info_container);
            holder.tv_remain = view.findViewById(R.id.tv_item_battery_info_remain);
            holder.tv_temperature = view.findViewById(R.id.tv_item_battery_info_temperature);
            holder.tv_status = view.findViewById(R.id.tv_item_battery_info_status);
            holder.tv_voltage = view.findViewById(R.id.tv_item_battery_info_voltage);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        BatteryItem item = dataList.get(i);
        holder.tv_title.setText("电池"+(i+1));
        holder.tv_remain.setText(item.getState().getChargeRemainingInPercent()+"%");
        holder.tv_temperature.setText(item.getState().getTemperature()+"°C");
        int status = item.getState().getConnectionState().value();
        if (status==ConnectionState.NORMAL.value()){
            holder.tv_status.setText("正常");
        }else if (status==ConnectionState.INVALID.value()){
            holder.tv_status.setText("无效");
        }else if (status==ConnectionState.EXCEPTION.value()){
            holder.tv_status.setText("异常");
        }else {
            holder.tv_status.setText("其他");
        }
        holder.tv_voltage.setText(item.getState().getVoltage()+"V");
        holder.tv_recycle_times.setText(item.getState().getLifetimeRemaining()+"");
        Integer[] cells = item.getCellVoltages();
        for (int j=0;j<cells.length;j++){
            View batteryView = LayoutInflater.from(context).inflate(R.layout.layout_battery_image_item,viewGroup,false);
            BatteryView bv = view.findViewById(R.id.btv_battery);
            bv.setPower(cells[j]);
            TextView textView = view.findViewById(R.id.tv_battery_v);
            textView.setText(cells[j]+"");
            holder.ll_container.addView(batteryView,j);
        }
        return null;
    }

    class ViewHolder {
        TextView tv_title;
        LinearLayout ll_container;
        TextView tv_remain;
        TextView tv_temperature;
        TextView tv_status;
        TextView tv_voltage;
        TextView tv_recycle_times;
    }
}
